var app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        data: '',
        dataList: '',
        auction_id: "",     // 活动id
    },
    // 关注
    attentionEvent() {
        var self = this;
        var url = "";
        var auctionData = this.data.data;
        var auction_id = this.data.auction_id;
        if (auctionData.auction.is_focus == 0) {
            url = "/focus/auction";
            auctionData.auction.is_focus = 1;
            auctionData.auction.num += 1
        } else {
            url = "/focus/auctioncancel";
            auctionData.auction.is_focus = 0;
            auctionData.auction.num -= 1
        }

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        id: auction_id
                    },
                    success: function (res) {
                        var list = res.data;
                        if (list.code == 1) {
                            self.setData({
                                data: auctionData
                            })
                        }
                    }
                })
            }
        })
    },
    // 委托出价
    entrustPrice(e){
        var auction_id = this.data.auction_id;
        var goods_id = e.target.dataset.id;
        wx.navigateTo({
            url: `../index-entrust/index-entrust?id=${goods_id}&auction_id=${auction_id}`
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
		this.dialog1 = this.selectComponent("#dialog1");		// 关注成功弹框
		this.dialog2 = this.selectComponent("#dialog2");		// 委托出价弹框

        var self = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                console.log("success")
                self.getAuctionInfo(res,options.id || 46)
            },
            fail() {
                wx.login({
                    success: res => {
                        console.log("fail")
                        wx.request({
                            url: app.url.requestUrl + '/login/login',
                            method: 'POST',
                            data: { code: res.code },
                            success: function (data) {
                                if (data.data.code == 1) {
                                    self.getAuctionInfo(data.data, options.id || 46)
                                }
                            }
                        })
                    }
                })
            }
        })
    },
    // 获取活动数据
    getAuctionInfo(res,id){
        var self = this;
        wx.request({
            url: app.url.requestUrl + '/auction/visAuctionList',
            method: 'POST',
            data: {
                openid: res.data.openid,
                token: res.data.token,
                auction_id: id
            },
            success: function (datas) {
                if (datas.data.code == 1) {
                    datas.data.data.auction.score = app.globalData.levelRuleCount(datas.data.data.auction.score)

                    self.setData({
                        auction_id: id,
                        data: datas.data.data,
                    })
                    wx.setNavigationBarTitle({ title: datas.data.data.auction.name || '一位不愿透露姓名的网友' })
                } else {
                    if (datas.data.code == -1 || datas.data.code == 404) {
                        self.loginOverdue();
                    }
                }
            }
        })
    },
    // 登录过期状态
    loginOverdue(e) {
        var self = this;
        wx.removeStorage({
            key: 'user',
            success: function (res) {
                self.onLoad();
            }
        })
    },
    // 分享
    onShareAppMessage(options) {
        var auction = this.data.data.auction;
        
        if (options.form == "button") {console.log("来自按钮")}

        return {
            title: '拍卖活动：' + auction.title,
            path: `pages/index-store/index-store?id=${auction.id}`
        }
    },
	// 关注
	focusActivify(e) {
		this.dialog1.showDialog();
	},
    // 委托出价
	entrustBid(){
		this.dialog2.showDialog();
	},
	
	// 关注成功 - 取消按钮
	__focusNoEvent(){
		this.dialog1.hideDialog();
	},
	// 关注成功 - 确认按钮
	__focusYesEvent() {
        this.attentionEvent();
		this.dialog1.hideDialog();
	},

	// 委托出价 - 取消按钮
	__entrustNoEvent(){
		this.dialog2.hideDialog();
	},
	// 委托出价 - 确认按钮
	__entrustYesEvent() {
		this.dialog2.hideDialog();
	},
})